// client/pages/repairHome/orderDetil/orderDetil.js
var app = getApp();
Page({
  data: {
    // 默认手机型号
    phoneModel: app.globalData.iphoneInfo.model,
    // 型号id
    modelId: '',
    // 备注
    textareaText: '',
    // 地址
    userAdress: '',
    // 手机号
    userPhone: '',
    // 用户姓名
    userName: '',
    // 故障名称
    malfunction: '',
    // 故障id
    malfunctionId: '',
    // 是否展示 上门地址服务
    isShowAdress: false,
    appointmentDate: '',
    appointmentTime: '',
    malfunctionIndex: 0,
    // 请求状态
    loading: false,
    disabled: false,
    // 故障列表
    malfunctionList: [],
    // 是否显示 手机输入框
    inputShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)

    // 界面 赋值
    this.setData({
      userAdress: app.globalData.address.address,
      phoneModel: options.modelName != undefined ? options.modelName : app.globalData.iphoneInfo.model,
      malfunction: options.malfunctionName != undefined ? options.malfunctionName : wx.$localStorage.get('malfunctionName')
    })

    // 获取手机 型号 id
    wx.$tool.getPhoneModelId(this.data.phoneModel).then(res => {
      this.setData({
        modelId: res,
      })
    })
    // 获取 故障Id
    this.getPhoneMaintenanceId()
    // 获取 故障列表
    this.setData({
      malfunctionList: wx.$localStorage.get('malfunctionList')
    })
  },
  // 获取 故障Id
  getPhoneMaintenanceId() {
    wx.$tool.getPhoneMaintenanceId(this.data.malfunction).then(res => {
      console.log(res)
      this.setData({
        malfunctionId: res,
      })
    })
  },
  /**
   * 打开地图
   */
  gotoAdress() {
    wx.openLocation({
      latitude: 30.284264,
      longitude: 120.125513,
      name: '古荡湾新村',
      scale: 28
    })
  },
  /**
   * 选择 日期
   */
  bindTimeChange(e) {
    this.setData({
      appointmentTime: e.detail.value
    })
  },
  bindDateChange(e) {
    this.setData({
      appointmentDate: e.detail.value
    })
  },
  /**
   * 选择要维修的手机型号
   */
  selectPhoneModel() {
    wx.navigateTo({
      url: '../phoneListSelect/phoneListSelect',
    })
  },
  /**
   * 选择手机故障
   */
  bindPickerChange: function(e) {
    this.setData({
      malfunctionIndex: e.detail.value,
      malfunction: this.data.malfunctionList[e.detail.value].repairName
    })
    this.getPhoneMaintenanceId()
  },
  /**
   * 提交订单
   */
  commitOrder(e) {
    console.log(e.detail.formId)
    // wx.$util.showToast(e.detail.formId)
    // 校验参数
    if (this.data.modelId.length == 0) {
      return wx.$util.showToast('请选择手机型号')
    } else if (this.data.malfunctionId.length == 0) {
      return wx.$util.showToast('请选择手机故障')
    } else if (this.data.userAdress.length == 0) {
      return wx.$util.showToast('请输入上门服务地址')
    } else if (this.data.userPhone.length == 0) {
      return wx.$util.showToast('请输入手机号，以便上门维修。')
    } else if (this.data.appointmentDate.length == 0) {
      return wx.$util.showToast('请选择预约日期')
    } else if (this.data.appointmentTime.length == 0) {
      return wx.$util.showToast('请选择预约时间')
    }
    let time = this.data.appointmentDate +' '+ this.data.appointmentTime +":00"
    // 组装参数
    if (e.detail.formId) {
      var obj = {
        phoneId: this.data.modelId,
        repairType: this.data.malfunctionId,
        address: this.data.userAdress,
        remark: this.data.textareaText,
        phoneNum: this.data.userPhone,
        formId: e.detail.formId,
        appointmentTime: time ? time : wx.$util.formatTime(new Date()),
      }
      // 提交订单
      wx.$api.submitPhoneOrderInfo(obj).then(res => {
        console.log(res.data)
        if (res.data.code == 10000) {
          wx.navigateTo({
            url: '../orderSuccess/orderSuccess',
          })
        } else {
          wx.$util.showToast('服务异常')
        }
      })
    }
  },
  /**
   * 输入框 事件
   */
  bindKeyInput: function(e) {
    console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == 0) {
      this.setData({
        userName: e.detail.value
      })
    } else if (e.currentTarget.dataset.type == 1) {
      this.setData({
        userPhone: e.detail.value
      })
    } else if (e.currentTarget.dataset.type == 2) {
      this.setData({
        userAdress: e.detail.value
      })
    } else if (e.currentTarget.dataset.type == 3) {
      this.setData({
        textareaText: e.detail.value
      })
    }
  },
  /**
   * 只有这个方法才能获取用户信息 
   */
  bindgetuserinfo: function(e) {
    console.log('获取用户信息')
    if (e.detail.userInfo) {
      wx.$localStorage.setCurrentUser(e.detail.userInfo)
      wx.$api.getUserPhone({
        nickName: e.detail.userInfo.nickName,
        headImage: e.detail.userInfo.avatarUrl
      }).then(res => {
        console.log(res)
      })
      this.setData({
        isShowAdress: true,
        userName: e.detail.userInfo.nickName
      })
    } else {
      wx.$util.showModel('温馨提示','请授权微信头像信息,才能提供上门服务,谢谢配合');
    }
  },
  /**
   * 获取用户 手机号
   */
  getPhoneNumber(e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    
    wx.$api.getUserPhone({
      ivPhoneData: e.detail.iv,
      phoneData: e.detail.encryptedData
    }).then(res => {
      console.log(res)
      if (!res){
        this.setData({
          inputShow: true        
        })
      }
    })
  }
})